wx.cloud.init({
  env: 'database-2p2zg',
  traceUser:true
})
const db = wx.cloud.database()
// orderBy方法规定排序
const req = db.collection('school-request').orderBy('date','desc')
const news = db.collection('news')
Page({
  data: {
    headerImages: [{ imgpath: 'cloud://database-2p2zg.6461-database-2p2zg/project_images/swiper/1.png' }, { imgpath: 'cloud://database-2p2zg.6461-database-2p2zg/project_images/swiper/2.png' }, { imgpath: 'cloud://database-2p2zg.6461-database-2p2zg/project_images/swiper/3.png' }],
    currentIndex: 0,
    navIcons: [{ imgpath: "/images/zhishi.png", name: "知识", url: '/pages/knowledge/knowledge' }, { imgpath: "/images/jineng.png", name: "技能", url: '/pages/ordinary/ordinary' },{ imgpath: "/images/tiyu.png", name: "运动",url:'/pages/social/social' }],
    demands: [],
    openid:''
  },
  pageNumber: { skip: 0 },  
  getData: function (callback) {
    this.data.openid =  wx.getStorageSync('openID')
    wx.showLoading({
      title: '正努力加载数据',
    })
  // res =>{}声明一个函数，传入res作为参数
    req.skip(this.pageNumber.skip).get().then(res => {
      let oldData = this.data.demands
      this.setData({ demands: oldData.concat(res.data), openid:this.data.openid })
      this.pageNumber.skip += 20
      res => {callback}
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var openid = wx.getStorageSync('openID')
    this.getData(res=>{})
    wx.setStorageSync('referID', [])
    // 判断你的评论是否被评论了
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * 直接调用数据库get()方法，成功实现了刷新数据的功能
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '正努力加载数据',
    })
    req.get().then(res => {
      this.setData({ demands: res.data })
      wx.hideLoading()
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   * 新建文件，搬代码解决无法触发的问题
   */
  onReachBottom: function () {
    this.getData()
  },

// 与onload不同只要页面在前台显示就会触发onshow，onload在初次创建时就会被全局出发一次
// onshow 能够实现数据的实时更新
  onShow:function(){
    var referID = wx.getStorageSync('referID')
    var openid = wx.getStorageSync('openID')
    req.where({
      _openid: openid
    }).get()
      .then(res => {
        var x
        for (x in res.data) {
          if (res.data[x].refer) {
            referID.push(res.data[x]._id)
            wx.showTabBarRedDot({
              index: 1,
            })
          }
        }
        wx.setStorageSync('referID', referID)
      })

    news.where({ acceptor: openid }).count()
      .then(res => {
        wx.setStorageSync('subComment', res.total)
        if (wx.getStorageSync('subComment') == 0) {
          wx.setStorageSync('oldSubComment', 0)
        }
      })
    // 判断你的评论是否有评论了
    if (wx.getStorageSync('subComment') > wx.getStorageSync('oldSubComment')){
      wx.showTabBarRedDot({
        index: 1,
      })
    }
  },

  toMessage: function () {
    var openid = this.data.openid
    wx.navigateTo({
      url: '/pages/message/message?openid='+openid,
    })
  },

  toHead: function(){
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },

  onShareAppMessage: function () {
    return {
      title: '校园互助，方便你我',
      path: 'pages/index/index',
    }
  }
})